package oop.ex1.filters;

import java.io.File;
import java.util.TreeSet;

//###############
//FILE : SizeFilter.java
//WRITER : Amir Wilf, wilfoa, 300007085
//WRITER : Daniel Acherkan, danithelion, 310116348
//EXERCISE : oop ex1 2011
//DESCRIPTION:
//A file filter which filter files by comparison to a given file size
//###############

public class SizeFilter extends OrderFilter{
	
	//************
    //DATA MEMBERS
    //************
	// the size of the file to filter by
	protected long _size;
	
	//*******
    //METHODS
    //*******
	/**
	 * A constructor to the SizeFilter
	 * @param direction 'true' for 'Greater', false for 'Before'.
	 * @param size the file size to compare to
	 */
	public SizeFilter(boolean direction,boolean not, String parameter) {
		super(direction,not);
		//parse the parameter to compare by
		parseParameter(parameter);
	}

	/**
	 * The method converts the size in String to a long size
	 * @param - size - the file size for comparison
	 */
	protected void parseParameter(String size) {
		long result=0;
		//transforming the Sting in to an integer
		for(int i=0;i<size.length();i++){
			result += (Math.pow(10, i)*
					((int)size.charAt(size.length()-1-i)-'0'));
		}
		_size = result;
	}
	
	/**
	 * The method lists all the files which answer the comparison by the size
	 * parameter
	 * @param directoryPath the path to filter files from
	 * @return A TreeSet of the filtered files
	 */
	public TreeSet<File> filter(TreeSet<File> fullDirectoryFiles){
		TreeSet<File> list = new TreeSet<File>();
		for(File file : fullDirectoryFiles){
			//case this is a GREATER filter:
			if(this._direction){
				if(file.length()> _size){
					list.add(file);
				}
			}
			//case this is a SMALLER filter:
			else{
				if(file.length()<=_size){
					list.add(file);
				}
			}
		}
		//taking the negation possibility into consideration:
		return addNegation(list,fullDirectoryFiles);
	}
}
